﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataStore.Domain;
using NHibernate;
using NHibernate.Linq;

namespace DataStore.Repositories
{
    /// <summary>
    /// Book repository
    /// </summary>
    public class BookRepository : IRepository<Book>
    {
        readonly UnitOfWork _unitOfWork;

        /// <summary>
        /// Initializes a new instance of the <see cref="BookRepository"/> class.
        /// </summary>
        public BookRepository()
        {
            UnityHelper.Container.Resolve<IDatabase>();
        }

        public BookRepository(UnitOfWork unitOfWork) : this()
        {
            _unitOfWork = unitOfWork;
        }

        #region IBookRepository Members

        public Book Get(object id)
        {
            ISession session = _unitOfWork.Session;
            return session.Get<Book>(id);
        }

        public void Delete(Book entity)
        {
            ISession session = _unitOfWork.Session;
            session.Delete(entity);
        }

        public void DeleteAll()
        {
            throw new NotImplementedException();
        }

        public void Save(Book entity)
        {
            ISession session = _unitOfWork.Session;
            session.Save(entity);
        }

        public Book SaveOrUpdate(Book entity)
        {
            ISession session = _unitOfWork.Session;
            session.SaveOrUpdate(entity);
            return entity;
        }

        /// <summary>
        /// Updates the specified book.
        /// </summary>
        /// <param name="book">The book.</param>
        public void Update(Book book)
        {
            ISession session = _unitOfWork.Session;
            session.Update(book); 
        }

        IEnumerable<Book> IRepository<Book>.All
        {
            get 
            {
                ISession session = _unitOfWork.Session;
                return session.Linq<Book>().ToList();
            }
        }

        public bool Exists(Book entity)
        {
            throw new NotImplementedException();
        }

        public bool IsEmpty
        {
            get { throw new NotImplementedException(); }
        }

        public int Count(Book entity)
        {
            throw new NotImplementedException();
        }

        public int CountAll()
        {
            throw new NotImplementedException();
        }

        

        /// <summary>
        /// Gets books by title.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <returns></returns>
        public ICollection<Book> GetByTitle(string title)
        {

            ISession session = _unitOfWork.Session;
            return session.Linq<Book>().Where(book => book.Title == title).ToList();
            
        }

        /// <summary>
        /// Gets books by author.
        /// </summary>
        /// <param name="author">The author.</param>
        /// <returns></returns>
        public ICollection<Book> GetByAuthor(Person author)
        {
            //using (ISession session = NHibernateHelper.OpenSession())
            //{
            //    return from b in session.Linq<Book>()
            //           join a in session.Linq<Authors>()
            //           on b.Authors == author.
            //           select b;
            //}
            throw new NotImplementedException();
        }


        #endregion
    }
}
